package com.ekode.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * Created by Administrator on 2015/2/12.
 */
@WebServlet(name = "Demo_001_Servlet")
public class Demo_001_Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        String message ="hello.....";

        System.out.print("你好：");
        PrintWriter printWriter = response.getWriter();
        printWriter.print(message);
        printContent(request,response);
        printWriter.flush();
        printWriter.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }

    public void  printContent(HttpServletRequest request, HttpServletResponse response)throws IOException{


        PrintWriter out = response.getWriter();


        out.println("请求的URI为："+request.getRequestURI()+"</br>");
        out.print("服务器Ip为："+request.getRemoteAddr()+"</br>");
        out.println("服务器名为："+request.getServerName()+"</br>");
        out.println("服务器端口号为："+request.getServerPort()+"</br>");
        out.println("协议名为："+request.getScheme()+"</br>");
        out.println("协议版本为："+request.getProtocol()+"</br>");
        out.println("请求方法为："+request.getMethod()+"</br>");

        out.println("上下文路径为："+request.getContextPath()+"</br>");
        out.println("Servlet路径为："+request.getServletPath()+"</br>");
        out.println(request.getLocalAddr());

        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()){
            String name = (String)e.nextElement();
            out.println(request.getHeader(name));
        }


    }
}
